/**
 * @author Edu
 */
	
var Skeleton = {
	
	scaffold : function(){ return new this.Class() },
	
	Extend : function(arg){
		return (
			arg
			? this.Class.prototype[arg]
			: this.Class.prototype
		)
	},
	
	Class : function(){
		// Private 
		var self = this
		var minify = false		
		var js = []		
		var packages = { 
			
			'url' : '/js/packages/',			
		
			'Tools' : 'Skeleton.tools.js',
			
			'Calendar':'Calendar/Calendar.js',
			'Pagination':'Pagination/Pagination.js',
		
			'jQuery':'jQuery/jQuery.js',
			'jQuery.validate':'jQuery/jQuery.validate.js',
			
			'Validator':'Validator/Validator.js',
			
			'Ajax':'Ajax/Ajax.js',
			'MVC' : 'MVC/MVC.js'
							 
		}
				
		var Import = function(){
			
			var url = []	
				
				for( var x = 0; x < arguments.length; x++ ) 
					url.push( arguments[x] )	
													
			var self = function( callback ){													
				
				if ( url.length ) {									
					
					var script = document.createElement('script')
						script.setAttribute('type', 'text/javascript')
						script.setAttribute('src', url[0] + (url[0].match(/nocache/) ? '=' + parseInt(Math.random() * 1000) : ''))
						script['onload'] = function(){
							
							if (url.length == 1) {
								url.shift()
								return callback ? callback() : null
							}
							
							url.shift()
							self( callback )
						}
						script['onreadystatechange'] = function(){
							if (script.readyState == 'loaded') 
								script['onload']()
						}
					
					document.getElementsByTagName('body')[0].appendChild(script)
				}									
			}
			
			return self
		}		
		
		// Public
		this.Minify = {
			'set' : function(url, separator, options){						
				minify = {
					url : url,
					separator : separator,
					options : (options ? options : '')
				}	
			}
		} 
		
		this.Packages = {
			':url' : function(url){ packages.url = url },
			':set' : function(name, value){ packages[name] = value }
		} 
					
		this.Controller = {
			':routes' : function(json){
				
				var url = location.href
				
				var pack = function(){
					for(var x = 0; x < arguments.length; x++)
						js.push(
							packages.url + 
							packages[ arguments[x] ] + '?nocache'
						)
				}
		
				var packjs = function(){
					for(var x = 0; x < arguments.length; x++)
						js.push(arguments[x] + '?nocache')
				}
							
				var set = function( aux ){
					for( var x in aux )
						switch( x ){
							case 'packages': pack.apply(null, aux[x]); break
							default : packjs.apply(null, aux[x])									
						}	
				}						
					
					for (var x in json)												
						if (url.match(x)) set(json[x])							
					
					if( minify ){
						var aux = '' 
							for(var x = 0; x < js.length; x++)
								aux +=  js[x]/*.split('\/').pop()*/ + minify.separator
						aux = aux.substring(0, aux.length - 1).replace(/\?nocache/gi,'')							
						Import.apply( null, [minify.url + aux + minify.options] )()								
						return 
					}	
					
				Import.apply(null, js)()
				
			}				
		}			
	}	
}		